<template>
  <div class="banner-list">
    <div class="banner-list-item" v-for="item in this.bannerList" :key="item.id">
      <img :src="item.name" />
      <i class="el-icon-close close-icon" @click="delImg(item.id)"></i>
    </div>
  </div>
</template>

<script>

export default {
  data() {
    return {
      bannerList: []
    }
  },
  async mounted() {
    const res = await this.$axios.get('/carousel/list')
    if (res.data?.list?.length) {
      this.bannerList.push(...res.data.list)
      this.bannerList.forEach(item => {
        item.name = window.axiosBaseUrl + '/' + item.name
      })
    }
  },
  methods: {
    delImgFn(id) {
      const userInfo = JSON.parse(sessionStorage.getItem('userInfo'))

      this.$axios.post('/carousel/backend/remove', {
        id: userInfo.id,
        imgId: id
      }).then(res => {
        if (res.data.code === 1) {
          this.bannerList.forEach((item, index) => {
            if (item.id === id) {
              this.bannerList.splice(index, 1)
            }
          })
          this.$message.success('删除成功')
        }
      })
    },
    delImg(id) {
      // 提示是否删除
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.delImgFn(id)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    }
  }
}

</script>

<style>
.banner-list {
  display: flex;
  height: 100%;
  flex-wrap: wrap;
  align-content: flex-start;
  padding: 10px;
}

.banner-list-item {
  width: 22%;
  height: 200px;
  margin-bottom: 20px;
  margin-right: 20px;
  position: relative;
}

img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.close-icon {
  position: absolute;
  top: 5px;
  right: 5px;
  background-color: rgba(255, 255, 255, 0.6);
  padding: 5px;
  font-weight: 600;
  border-radius: 50%;
  cursor: pointer;
}
</style>
